//
//  RosterCell.m
//  Gabber
//
//  Created by 惠国 陆 on 11-9-19.
//  Copyright 2011年 恒生聚源. All rights reserved.
//

#import "RosterCell.h"
#import "XMPPClient.h"

@implementation RosterCell

@synthesize nameLabel=_nameLabel;
@synthesize statusLabel=_statusLabel;
@synthesize avatarView=_avatarView;
@synthesize showView=_showView;
@synthesize user=_user;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)dealloc {
    TT_RELEASE_SAFELY(_nameLabel);
    TT_RELEASE_SAFELY(_statusLabel);  
    TT_RELEASE_SAFELY(_avatarView);
    TT_RELEASE_SAFELY(_showView);    
    TT_RELEASE_SAFELY(_user);    
    [super dealloc];
}



- (UILabel*)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.font = TTSTYLEVAR(tableFont);
        _nameLabel.textColor = TTSTYLEVAR(textColor);
        _nameLabel.highlightedTextColor = TTSTYLEVAR(highlightedTextColor);
        _nameLabel.backgroundColor = TTSTYLEVAR(backgroundTextColor);
        _nameLabel.textAlignment = UITextAlignmentLeft;
        _nameLabel.lineBreakMode = UILineBreakModeTailTruncation;
        _nameLabel.adjustsFontSizeToFitWidth = YES;        
        [self.contentView addSubview:_nameLabel];
    }
    return _nameLabel;
}

- (UILabel*)statusLabel {
    if (!_statusLabel) {
        _statusLabel = [[UILabel alloc] init];
        _statusLabel.font = TTSTYLEVAR(font);
        _statusLabel.textColor = TTSTYLEVAR(tableSubTextColor);
        _statusLabel.highlightedTextColor = TTSTYLEVAR(highlightedTextColor);
        _statusLabel.backgroundColor = TTSTYLEVAR(backgroundTextColor);
        _statusLabel.textAlignment = UITextAlignmentLeft;
        _statusLabel.contentMode = UIViewContentModeTop;
        _statusLabel.lineBreakMode = UILineBreakModeTailTruncation;
        _statusLabel.numberOfLines = kTableMessageTextLineCount;        
        [self.contentView addSubview:_statusLabel];
    }
    return _statusLabel;
}

- (UIImageView*)avatarView {
    if (!_avatarView) {
        _avatarView = [[UIImageView alloc] init];
        [self.contentView addSubview:_avatarView];
    }
    return _avatarView;
}

- (UIImageView*)showView {
    if (!_showView) {
        
        _showView = [[UIImageView alloc] init];
        _showView.contentMode = UIViewContentModeCenter;
        [self.contentView addSubview:_showView];
    }
    return _showView;
}


- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat height = self.contentView.height;
    CGFloat width = self.contentView.width - (height + kTableCellSmallMargin)- (16 + kTableCellSmallMargin);
    CGFloat left = 0;
    
    if (_avatarView) {
        _avatarView.frame = CGRectMake(0, 0, height, height);
        left = _avatarView.right + kTableCellSmallMargin;
        
    } else {
        left = kTableCellHPadding;
    }
    
    self.showView.frame = CGRectMake(left, 0, 16, height);
    left = self.showView.right + kTableCellSmallMargin;
    
    
    if (self.statusLabel.text.length) {
        CGFloat nameHeight = self.nameLabel.font.ttLineHeight;
        CGFloat statusHeight = self.statusLabel.font.ttLineHeight;
        CGFloat paddingY = floor((height - (nameHeight + statusHeight))/2);
        
        
        self.nameLabel.frame = CGRectMake(left , paddingY, width , nameHeight);
        self.statusLabel.frame = CGRectMake(left, self.nameLabel.bottom, width, statusHeight);
        
    } else {
        self.nameLabel.frame = CGRectMake(left, 0, width, height);
        self.statusLabel.frame = CGRectZero;
    }
}

- (void)setUser:(XMPPUserCoreDataStorageObject *)user {
    if (_user != user) {
        [_user release];
        _user = [user retain];
        if (user) {
            @try {
                self.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
                if (user.displayName.length) {
                    self.nameLabel.text = [NSString stringWithFormat:@"%@", user.displayName];
                }
                if (user.primaryResource && user.primaryResource.status) {
                    self.statusLabel.text = user.primaryResource.status;
                }
                
                UIImage* online = [UIImage imageNamed:@"online"];
                UIImage* away = [UIImage imageNamed:@"away"];
                UIImage* offline = [UIImage imageNamed:@"offline"];
                

                self.avatarView.image = [[XMPPClient sharedInstance] avatarForUser:user];
                
                
                switch (user.section)
                {
                    case 0:
                        self.showView.image=online;
                        break; 
                    case 1:
                        self.showView.image=away;
                        break; 
                    default:
                        self.showView.image=offline;
                        break;               
                }

            }
            @catch (NSException *exception) {
                DDLogWarn(@"Exception: %@", exception);
            }
            @finally {
                
            }
            
        }
                
    }
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (void)prepareForReuse
{
    self.user = nil;
    self.nameLabel.text = nil;
    self.statusLabel.text = nil;
    self.avatarView.image = nil;
    self.showView.image = nil;
    [super prepareForReuse];
    
    
}

@end
